<template>
<div class="f-menu" :style="{ width:$store.state.asideWidth }">
  <el-menu class="border-0" :default-active="defaultActive" unique-opened :collapse="isCollapse" :collapse-transition="false" @select="handelSelect" >
    <template v-for="(item,index) in asideMenus" :key="index">
      <el-sub-menu v-if="item.child && item.child.length >0" :index="item.name">
        <template #title>
          <el-icon><component :is="item.icon"></component></el-icon>
          <span>{{ item.name }}</span>
        </template>
        <el-menu-item v-for="(item2,index2) in item.child" :key="index2" :index="item2.front_path">
          <el-icon><component :is="item2.icon"></component></el-icon>
          <span>{{ item2.name }}</span>
        </el-menu-item>
      </el-sub-menu>
      <el-menu-item v-else :index="item.frontpath">
        <el-icon><component :is="item.icon"></component></el-icon>
        <span>{{ item.name }}</span>
      </el-menu-item>
    </template>
   
 
  </el-menu>
</div>
</template>

<script setup>
import { useRouter,useRoute,onBeforeRouteUpdate } from 'vue-router'
import { useStore } from 'vuex' 
import { computed,onMounted,ref } from 'vue' 
import { sidemenu } from '~/api/admin/manager.js'

/* const getmenu=()=>{
  sidemenu()
  .then(res=>{
    console.log(res);
  })
}
getmenu() */

const router = useRouter()
const store = useStore()
const route = useRoute()

//是否折叠侧边栏
const isCollapse = computed(() => !(store.state.asideWidth == '250px')) 

//默认选中侧边二级菜单
const defaultActive = ref(route.path)

/* const asideMenus = [{
  name : '后台面板',
  icon:'help',
  child:[{
    name : '后台面板',
    icon:'help',
    frontpath:'/admin'
  }]
}] */
//const asideMenus = computed(()=>store.state.menus.filter(b => b.parent_menu == null))
const asideMenus = computed(()=>store.state.menus)
//console.log(asideMenus);
//将数组整理成递归格式
/* function buildTree(items, parent_menu = null) {  
  let children = items.filter(item => item.parent_menu === parent_menu).map(item => {  
    const childItems = buildTree(items, item.id);  
    return childItems.length ? { ...item, children: childItems } : item;  
  });  
  return children.length ? children : [];  
}  
const asideMenus = buildTree(asideMenu.value);  */


const handelSelect =(e)=>{
  //console.log(e);
  router.push(e)
}


</script>

<style lang='scss'>
.f-menu {
  @apply shadow-md fixed bg-light-50;
  top: 64px;
  bottom: 0;
  left: 0;
  overflow-y: auto;
  overflow-x: hidden;
  transition: all 0.2s; //动画效果  过渡2秒
}
.f-menu::-webkit-scrollbar {
  width: 0;
}
</style>